import logging
from django.db.models import F
from django.db import transaction


def add_hot(table, pk, formula):
    """热度增加"""
    """
    models.Release.objects.filter(id=reply_id).update(comment_num=F('hot') + config.COMMENT_NUM)
    """

    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(hot=F('hot') * formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('热度增加失败')
            return '热度增加失败'


def add_comment_num(table, pk, formula):
    """热度增加"""
    """
    models.Release.objects.filter(id=release_id).update(comment_num=F('comment_num') + config.COMMENT_NUM)
    """
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(comment_num=F('comment_num') + formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('评论数量增加失败')
            return '评论数量增加失败'


def del_thumbs_up(table, pk, formula):
    """动态表点赞数量减一"""
    """
models.Release.objects.filter(id=release_id, is_delete=False, is_show=True).update(thumbs_up=F('thumbs_up') - config.COMMENT_NUM)
    """
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(thumbs_up=F('thumbs_up') - formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('点赞数量减少失败')
            return '点赞数量减少失败'


def add_thumbs_up(table, pk, formula):
    """动态表点赞数量加一"""
    """
models.Release.objects.filter(id=release_id, is_delete=False, is_show=True).update(thumbs_up=F('thumbs_up') - config.COMMENT_NUM)
    """
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(thumbs_up=F('thumbs_up') + formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('点赞数量增加失败')
            return '点赞数量增加失败'


def add_fans(table, pk, formula):
    """粉丝数量数量加一"""
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(fans=F('fans') + formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('粉丝数量数量增加失败')
            return '粉丝数量数量增加失败'


def del_fans(table, pk, formula):
    """粉丝数量数量数量减一"""
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(fans=F('fans') - formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('粉丝数量数量减少失败')
            return '粉丝数量数量减少失败'


def add_follow(table, pk, formula):
    """关注者数量加一"""
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(follow=F('follow') + formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('关注者数量增加失败')
            return '关注者数量增加失败'


def del_follow(table, pk, formula):
    """关注者数量加一"""
    with transaction.atomic():
        # 逻辑错误回滚
        sid = transaction.savepoint()
        try:
            table.objects.filter(id=pk, is_delete=False, is_show=True).update(follow=F('follow') - formula)
        except:
            transaction.savepoint_rollback(sid)
            logging.error('关注者数量减少失败')
            return '关注者数量减少失败'
